package com.mtsmda.chapter5.exeptionEx;

import java.io.IOException;

import sun.applet.AppletIOException;

public class RunExceptions {

	public static void main(String[] args) {
		try {
			new RunExceptions().test();
		} catch (RuntimeException e) {
			System.out.println("ERROR! '" + e.getLocalizedMessage() + "'");
		}

		System.out.println("-------------------------");

		try {
			new RunExceptions().checkedMyException();
		} catch (MyFirstException e) {
			System.out.println("ERROR - MyFirstException! '"
					+ e.getLocalizedMessage() + "'");
		}

		System.out.println("-------------------------");
		// badMethod();

		System.out.println("-------------------------");
		new RunExceptions().go();// java.lang.StackOverflowError
	}

	void test() {
		System.out.println("begin test");
		throw new RuntimeException("This is runtime exception");
		// System.out.println("end test"); //Unreachable code
	}

	void checkedMyException() throws MyFirstException {
		System.out.println("begin checkedMyException()");
		throw new MyFirstException("this is my first exception");
		// System.out.println("begin checkedMyException()"); //Unreachable code
	}

	static void someMethod() {
		try {
			stuff();
		} catch (MyFirstException e) {
			e.printStackTrace();
		}
	}

	static void stuff() throws MyFirstException {
		try {
			throw new MyFirstException();
		} catch (MyFirstException e) {
			System.out.println(e.getMessage());
			throw e;
		}
	}

	static void badMethod() {
		throw new Error("Error");
	}

	private void go() {
		go();
	}

}

class TestException4 {

	public void start() throws IOException {
	}

	public void foo() throws NullPointerException {

	}
}

class TestException5 extends TestException4 {

	public void start() throws AppletIOException {
	}

	public void foo() throws RuntimeException {

	}
}